#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 1e5+10;

queue<int> q;
int h[N], e[N], ne[N], idx, st[N];
int n, m;

int add(int a, int b)
{
    //邻接表头插
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int bfs(int u)
{
    if(n == 1) return 0;
    q.push(1);
    while(!q.empty())
    {
        auto ee = q.front();
        q.pop();
        for(int i = h[ee]; ~i; i = ne[i])
        {
            int pos = e[i];
            if(!st[pos])
            {
                //如果还没走过，入队列,并且记录distance(标记已经走过了)
                q.push(pos);
                st[pos] = st[ee] + 1;
                if(pos == n) return st[pos];
            }
        }
    }
    return -1;
}

int main()
{
    cin >> n >> m;
    memset(h,-1,sizeof h);
    for(int i = 0; i < m; ++i)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
    }
    cout << bfs(1) << endl;
    return 0;
}